package edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.domain.OrdersItem;
import edu.dto.OrdersItemDto;
import edu.service.GoodsService;
import edu.service.OrdersItemService;
import edu.support.base.controller.AdminBaseController;
import edu.support.mybatis.MybatisWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * (OrdersItem)表控制层
 *
 * @date 20:03:45
 */

@Controller
@RequestMapping("ordersItem")
public class OrdersItemController extends AdminBaseController {

    @Autowired
    private OrdersItemService ordersItemService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 我的购物车
     *
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Model model) {
        QueryWrapper<OrdersItem> wrapper = new MybatisWrapper<OrdersItem>()
                .eq("oi.orders_id", 0)
                .eq("oi.user_id", loginUserId())
                .orderByDesc("oi.create_time");
        List<OrdersItemDto> dtoList = ordersItemService.selectDto(wrapper);
        model.addAttribute("dtoList", dtoList);

        double total = 0D;
        // 总价
        for (OrdersItemDto dto : dtoList) {
            total += dto.getNum() * dto.getGoodsMoney();
        }
        model.addAttribute("total", total);

        return "ordersItem/list";
    }


    /**
     * 删除购物车
     *
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public String del(@PathVariable Long id, RedirectAttributes attributes) {
        ordersItemService.removeById(id);
        initCartNum();
        return refresh("删除成功", attributes);
    }


    /**
     * 添加购物车
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addItem/{id}")
    public String addItem(@PathVariable Long id, Integer count, RedirectAttributes model) {
        OrdersItem ordersItem = ordersItemService.getOne(new QueryWrapper<OrdersItem>()
                .eq("user_id", loginUserId())
                .eq("orders_id", 0)
                .eq("goods_id", id));
        if (null == ordersItem) {
            ordersItem = new OrdersItem();
            ordersItem.setUserId(loginUserId());
            ordersItem.setGoodsId(id);
            ordersItem.setNum(count);
            ordersItemService.save(ordersItem);
        } else {
            ordersItem.setNum(ordersItem.getNum() + count);
            ordersItemService.updateById(ordersItem);
        }
        initCartNum();
        return refresh("添加成功", model);
    }

    /**
     * 更新
     *
     * @param ordersItem
     * @return
     */
    @RequestMapping("update")
    public String update(OrdersItem ordersItem, RedirectAttributes attributes) {
        ordersItemService.saveOrUpdate(ordersItem);
        initCartNum();
        return refresh();
    }


}


